//
//  ViewController.m
//  MutableLanguage
//
//  Created by bj_szd on 2025/9/3.
//

#import "ViewController.h"
#import "LanguageManager.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self updateUI];
    
    // 监听语言改变通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(languageChanged:)
                                                 name:@"LanguageChanged"
                                               object:nil];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor systemBackgroundColor];
    
    // 欢迎标签
    self.welcomeLabel = [[UILabel alloc] init];
    self.welcomeLabel.font = [UIFont boldSystemFontOfSize:24];
    self.welcomeLabel.textAlignment = NSTextAlignmentCenter;
    self.welcomeLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.welcomeLabel];
    
    // 问候标签
    self.helloLabel = [[UILabel alloc] init];
    self.helloLabel.font = [UIFont systemFontOfSize:18];
    self.helloLabel.textAlignment = NSTextAlignmentCenter;
    self.helloLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.helloLabel];
    
    // 当前语言标签
    self.currentLanguageLabel = [[UILabel alloc] init];
    self.currentLanguageLabel.font = [UIFont systemFontOfSize:16];
    self.currentLanguageLabel.textAlignment = NSTextAlignmentCenter;
    self.currentLanguageLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.currentLanguageLabel];
    
    // 语言选择分段控制器
    NSArray *languages = [[LanguageManager sharedManager] supportedLanguages];
    NSMutableArray *languageNames = [NSMutableArray array];
    for (NSString *language in languages) {
        [languageNames addObject:[[LanguageManager sharedManager] displayNameForLanguage:language]];
    }
    
    self.languageSegmentedControl = [[UISegmentedControl alloc] initWithItems:languageNames];
    self.languageSegmentedControl.translatesAutoresizingMaskIntoConstraints = NO;
    [self.languageSegmentedControl addTarget:self
                                      action:@selector(languageSegmentedControlChanged:)
                            forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.languageSegmentedControl];
    
    // 设置当前选中的语言
    NSString *currentLanguage = [[LanguageManager sharedManager] currentLanguage];
    NSInteger selectedIndex = [languages indexOfObject:currentLanguage];
    if (selectedIndex != NSNotFound) {
        self.languageSegmentedControl.selectedSegmentIndex = selectedIndex;
    }
    
    // 切换语言按钮
    self.changeLanguageButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.changeLanguageButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.changeLanguageButton addTarget:self
                                  action:@selector(changeLanguageButtonTapped:)
                        forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.changeLanguageButton];
    
    [self setupConstraints];
}

- (void)setupConstraints {
    [NSLayoutConstraint activateConstraints:@[
        // 欢迎标签约束
        [self.welcomeLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        [self.welcomeLabel.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:50],
        
        // 问候标签约束
        [self.helloLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        [self.helloLabel.topAnchor constraintEqualToAnchor:self.welcomeLabel.bottomAnchor constant:20],
        
        // 当前语言标签约束
        [self.currentLanguageLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        [self.currentLanguageLabel.topAnchor constraintEqualToAnchor:self.helloLabel.bottomAnchor constant:30],
        
        // 语言选择分段控制器约束
        [self.languageSegmentedControl.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        [self.languageSegmentedControl.topAnchor constraintEqualToAnchor:self.currentLanguageLabel.bottomAnchor constant:30],
        [self.languageSegmentedControl.leadingAnchor constraintGreaterThanOrEqualToAnchor:self.view.leadingAnchor constant:20],
        [self.languageSegmentedControl.trailingAnchor constraintLessThanOrEqualToAnchor:self.view.trailingAnchor constant:-20],
        
        // 切换语言按钮约束
        [self.changeLanguageButton.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        [self.changeLanguageButton.topAnchor constraintEqualToAnchor:self.languageSegmentedControl.bottomAnchor constant:30],
        [self.changeLanguageButton.widthAnchor constraintEqualToConstant:200],
        [self.changeLanguageButton.heightAnchor constraintEqualToConstant:44]
    ]];
}

- (void)updateUI {
    LanguageManager *manager = [LanguageManager sharedManager];
    
    self.welcomeLabel.text = [manager localizedStringForKey:@"欢迎"];
    self.helloLabel.text = [manager localizedStringForKey:@"hello"];
    self.currentLanguageLabel.text = [NSString stringWithFormat:@"%@: %@", 
                                     [manager localizedStringForKey:@"current_language"],
                                     [manager displayNameForLanguage:[manager currentLanguage]]];
    [self.changeLanguageButton setTitle:[manager localizedStringForKey:@"change_language"] 
                               forState:UIControlStateNormal];
}

- (void)languageChanged:(NSNotification *)notification {
    [self updateUI];
}

- (void)languageSegmentedControlChanged:(UISegmentedControl *)sender {
    NSArray *languages = [[LanguageManager sharedManager] supportedLanguages];
    NSString *selectedLanguage = languages[sender.selectedSegmentIndex];
    [[LanguageManager sharedManager] setLanguage:selectedLanguage];
}

- (void)changeLanguageButtonTapped:(UIButton *)sender {
    // 显示语言选择提示和调试信息
    LanguageManager *manager = [LanguageManager sharedManager];
    NSString *currentLang = [manager currentLanguage];
    NSString *savedLang = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppLanguage"];
    
    NSString *message = [NSString stringWithFormat:@"当前语言: %@\n保存的语言: %@", 
                        currentLang,
                        savedLang ?: @"nil"];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"语言调试信息"
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定"
                                                       style:UIAlertActionStyleDefault
                                                     handler:nil];
    [alert addAction:okAction];
    
    [self presentViewController:alert animated:YES completion:nil];
}

@end
